<%@ page import="org.jeecgframework.web.util.StaticValue" %>
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/context/mytags.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <title>巡检预约 - 创建</title>
    <t:base type="jquery,easyui,tools,DatePicker"></t:base>
    <script type="text/javascript" src="plug-in/ckfinder/ckfinder.js"></script>
</head>
<body>
<t:formvalid formid="formobj" dialog="false" usePlugin="password" layout="table"
             action="cbsInspectionAppointmentController.do?doAdd" tiptype="3" callback="subCallBack">
    <input type="hidden" name="status" id="status">
    <input type="hidden" name="id" value="${bus.id }">
    <div class="subtitle">
        <p>客户信息</p>
        <table class="nobordertable noborder">
            <c:if test="${not empty inAppId}">
                <tr>
                    <td class="al_r">
                        <label>巡检预约服务ID:</label>
                    </td>
                    <td>
                            ${inAppId}
                    </td>
                    <td class="al_r"></td>
                    <td></td>
                </tr>
            </c:if>
            <tr>
                <td class="al_r">
                    <label>影院名称:</label>
                </td>
                <td>
                    <input type="hidden" name="cinema.id" id="cinemaId" value="${bus.cinema.id }"/>
                    <input type="hidden" name="isInService" id="isInService" value="1"/>
                    <input type="text" name="cinemaName" id="cinemaName" readonly="readonly" value="${bus.cinema.name }"
                           dataType="*">
                    <t:choose url="zyCinemaController.do?zyChooseCmOfContact" name="cinemaList" icon="" height="450px"
                              title="选择影院" hiddenName="cinemaId" inputTextname="cinemaName" hiddenid="id"
                              textname="name" fun="clearEquip"/>
                </td>
                <td class="al_r">
                    <label>联系人姓名:</label>
                </td>
                <td>
                    <input type="hidden" name="linkmanId" id="linkmanId"/>
                    <input type="text" name="contactName" id="contactName" dataType="*" value="${bus.contactName }">
                    <t:choose url="cbsProcurementNoteController.do?zyChooseContactersList" name="contacterList" icon=""
                              height="450px" width="900px" paramIds="cinemaId"
                              title="选择联系人" hiddenName="linkmanId" inputTextname="contactName" hiddenid="id"
                              textname="name" fun="getContactById"/>
                </td>
            </tr>
            <tr>

                <td class="al_r">
                    <label>电话:</label>
                </td>
                <td>
                    <input type="text" name="phone" id="phone" dataType="cPhone" value="${bus.phone }">
                </td>
                <td class="al_r">
                    <label>邮箱：</label>
                </td>
                <td>
                    <input type="text" name="mail" id="mail" dataType="e" value="${bus.mail }">
                </td>
            </tr>
            <tr>

                <td class="al_r">
                    <label>期望巡检季度:</label>
                </td>
                <td>
                    <select name="expectDate" style="width:68px;">
                        <option value="">年份</option>
                        <c:forEach var="year" items="${years}">
                            <option value="${year}-01-01"
                            <c:if test="${year eq expectDate}">
                                selected
                            </c:if>
                            >${year}</option>
                        </c:forEach>
                    </select>
                    <select name="expectSection" style="width:80px;" id="expectSection">
                        <option value="">季度</option>
                    </select>
                        <%--<t:dictSelect field="expectSection" defaultVal="${bus.expectSection}" typeGroupCode="<%=StaticValue.DIC_APPOINT_EXPECT_SECTION%>" extendJson="{datatype:'*1-2',nullmsg:'请选择巡检季度'}"></t:dictSelect>--%>
                    <%--<input type="text" name="expectDate" id="expectDate" dataType="*0-50"
                           value='<fmt:formatDate value="${bus.expectDate }" pattern="yyyy-MM-dd"/>' class="Wdate"
                           onfocus="WdatePicker({minDate:'%y-%M-%d'})">--%>
                </td>
                <td class="al_r">
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td class="al_r">
                    <label>说明：</label>
                </td>
                <td colspan="3">
                    <textarea rows="3" style="width:80%" name="content">${bus.content }</textarea>
                </td>
            </tr>
        </table>
    </div>

    <div class="subtitle">
        <p>相关设备</p> &nbsp;<a href="#" class="a-btn"
                             onclick="if($('#cinemaId').val()==''){$.Showmsg('请先选择影院');return false;}choose_equipmentList();">添加设备</a>
        <input type="hidden" id="equipmentId">
        <input type="hidden" id="equipmentSn">
    </div>
    <table style="line-height: 35px;" cellpadding="0" cellspacing="1" class="formtable" id="equipmentTab">
        <tr align="center">
            <td>设备SN</td>
            <td>型号</td>
            <td>机型</td>
            <td>发货日期</td>
            <td>安装日期</td>
            <td>保期截至日期</td>
            <td>状态</td>
            <td>操作</td>
        </tr>
        <c:if test="${empty bus }">
            <tr align="center" style="background-color: #FFFFFF;" id="equipmentNullmsg">
                <td colspan="8">请添加设备</td>
            </tr>
        </c:if>
        <tbody id="equipBody">
        <c:forEach items="${bus.esList }" var="es">
            <tr align="center" style="background-color: #FFFFFF;">
                <input type="hidden" name="equipmentIdStr" value="${es.es.id }">
                <input type="hidden" name="equipmentTypeStr" value="${es.type }">
                <td>${es.es.sn }</td>
                <td>${es.es.item }</td>
                <td>${es.es.type.typeDesc }</td>
                <td><fmt:formatDate value="${es.es.sendDate }" pattern="yyyy-MM-dd"/></td>
                <td><fmt:formatDate value="${es.es.installDate }" pattern="yyyy-MM-dd"/></td>
                <td><fmt:formatDate value="${es.es.repairEndDate }" pattern="yyyy-MM-dd"/></td>
                <td>
                    <c:if test="${es.type eq '0'}">无巡检计划</c:if>
                    <c:if test="${es.type eq '1'}">有巡检计划</c:if>
                    <c:if test="${es.type eq '2'}">本年度已巡检</c:if>
                </td>

                <td><a href="#" onclick="delrow(this)">删除</a></td>
            </tr>
        </c:forEach>
        </tbody>

    </table>
    <t:choose url="cbsProcurementPartEsController.do?zyEquipmentSpareInfoSelList" name="zyEquipmentSpareList" icon=""
              height="450px" butishidden="true" clickName="equipmentList"
              paramIds="cinemaId,isInService" title="选择设备" hiddenName="equipmentId" inputTextname="equipmentSn"
              hiddenid="id" textname="sn" fun="add_equipment"/>
</t:formvalid>
<div style="text-align: center;margin: 10px;">
    <a class="easyui-linkbutton" onclick="save(0)">保存</a>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <a class="easyui-linkbutton" onclick="save(4)">提交</a>
    <c:if test="${not empty bus.id }">
        &nbsp;&nbsp;&nbsp;&nbsp;
        <a class="easyui-linkbutton" onclick="save(3)">取消</a>
    </c:if>
</div>
</body>
<script type="text/javascript">
    var cinemaId = "";
    var eSection = ${expectSection} || 0;
    function expectDateChange(){
        var _this = $("select[name='expectDate']");
        var _o =$("select[name='expectSection']");
        _o.children().not(":first").remove();
        if(_this.val()==''){
            _o.children().not(":first").remove();
            _o.children(":first").attr("selected","selected");
            return;
        }
        $.getJSON("cbsInspectionAppointmentController.do?getExpectionByType&value="+_this.val().substring(0,4),{},function(data){
            var optionArray = new Array();
            $.each(data,function(idx,itm){
                optionArray.push("<option value='"+itm.index+"'>"+itm.name+"</option>")

            })
            _o.append(optionArray.join(""));
            if(eSection > 0){
                _o.find("[value='"+eSection+"']").attr("selected","selected");
                eSection = 0;
            }else{
                if(optionArray.length ==1){
                    _o.children(":last").attr("selected","selected");
                }
            }

        })
    }
    $(function(){
        /*if(eYear != ''){
            $("select[name='expectDate']").find("[value='"+eYear+"']").attr("selected","selected");
            expectDateChange();
            return;
        }*/
        expectDateChange();
        $("select[name='expectDate']").change(function(){
            expectDateChange();
        });

        $("select[name='expectSection']").change(function(){
            var selValue = $(this).val();
            if(isNaN(selValue) || selValue == ''){
                return ;
            }
            $.getJSON("cbsInspectionAppointmentController.do?checkSection",{section:selValue,expectDate:$("select[name='expectDate']").val()},function(data){
                if(!data.success){
                    $.Showmsg(data.msg);
                }
            })
        })
    })
    function save(type) {
        $('#status').val(type);
        if(type==3){
            $.messager.confirm('确认','确认取消该预约吗',function(r){
                if(r){
                    $('#formobj').submit();
                    return;
                }
            });
            return;
        }
        if ($.isEmptyObject($("#expectSection").val())) {
            $.Showmsg('请选择期望巡检季度');
            return;
        }

        var esSize = $('input[name="equipmentIdStr"]').length;
        if (esSize < 1) {
            $.Showmsg('至少选择一条设备');
            return;
        }
        var inAppoint = "";
        $.ajax({
            url: "cbsInspectionAppointmentController.do?existsAppoint",
            data: {
                snNum: $("input[name='equipmentIdStr']").val()
            },
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.success) {
                    inAppoint = data.msg;
                }
            }
        })
        if (!$.isEmptyObject(inAppoint)) {
            $.messager.alert('提示', '您选择的设备:' + inAppoint + "目前正在预约审批中，请不要重复提交");
            return;
        }

        var sn = '';
        $('input[name="equipmentTypeStr"]').each(function () {
            if ($(this).val() == 1 || $(this).val() == 2) {
                sn += $(this).next().html() + "、"
            }
        })

        if (sn != '') {
            var c = $.dialog.confirm('您提交的设备' + sn + '已经在本年度巡检计划中、请确认是否要提交巡检申请?', function () {
                $('#formobj').submit();
            });
            return;
        } else {
            $('#formobj').submit();
        }
    }
    function clearEquip() {
        $("#linkmanId").val('');
        $("#contactName").val('');
        $("#phone").val('');
        $("#contactEmail").val('');
        $("#equipBody").children().remove();
    }
    function subCallBack(data) {

        if(data.success){
            top.tip(data.msg);
            if ('${bus.id }' != "") {
                //刷新列表
                var win = frameElement.api.opener;
                win.reloadTable();
                frameElement.api.close();
            } else {
                top.$('#maintabs').tabs('select', '巡检申请');
                var tab = top.$('#maintabs').tabs("getSelected");
                tab.panel("refresh");
                closeCurrentTab('创建巡检申请');
            }
        }else{
            $.Showmsg(data.msg);
        }
    }


    function getCinemaById() {
        var id = $('#cinemaId').val();
        $.post('cbsProcurementPartEsController.do?getCinemaById', {id: id}, function (data) {
            data = $.parseJSON(data);
            $('#cinemaAddr').text(data.cinema.address == null ? "" : data.cinema.address);
            $('#departName').text(data.depart == null ? "" : data.depart);
            $('#city').text(data.city == null ? "" : data.city);
        });
    }

    function getContactById() {
        var id = $('#linkmanId').val();
        $.post('cbsProcurementPartEsController.do?getContactById', {id: id}, function (data) {
            data = $.parseJSON(data);
            $('#phone').val(data.cellphone1 == null ? "" : data.cellphone1);
            $('#mail').val(data.email == null ? "" : data.email);
        });
    }

    function add_equipment() {

        $('#equipmentNullmsg').remove();
        var id = $('#equipmentId').val();
        $('input[name="equipmentIdStr"]').each(function () {
            if (id.indexOf($(this).val()) != -1) {
                id = id.replace($(this).val(), '');
            }
        })
        $.post('cbsPartInfoController.do?getEquipmentIdById', {id: id}, function (data) {
            data = $.parseJSON(data);
            var str = "";
            for (var i = 0; i < data.length; i++) {
                str += '<tr align="center" style="background-color: #FFFFFF;">';
                str += '<input type="hidden" name="equipmentIdStr" value="' + data[i].id + '">';
                str += '<input type="hidden" name="equipmentTypeStr" value="' + data[i].inspectionType + '">';
                str += '<td>' + data[i].sn + '</td>';
                str += '<td>' + data[i].item + '</td>';
                str += '<td>' + data[i].type + '</td>';
                str += '<td>' + data[i].sendDate + '</td>';
                str += '<td>' + data[i].installDate + '</td>';
                str += '<td>' + data[i].repairEndDate + '</td>';
                //0.无巡检计划 1.有巡检计划 2.本年度已巡检
                if (data[i].inspectionType == 0) {
                    str += '<td>无巡检计划</td>';
                } else if (data[i].inspectionType == 1) {
                    str += '<td>有巡检计划</td>';
                } else if (data[i].inspectionType == 2) {
                    str += '<td>本年度已巡检</td>';
                }
                str += '<td><a href="#" onclick="delrow(this)">删除</a></td>';
                str += '</tr>';
            }
            $(str).appendTo("#equipBody");

        });
    }

    function delrow(del) {
        $(del).parent().parent().remove();
    }
</script>